package com.zoo.diannaopeijian.controller;

import com.zoo.diannaopeijian.common.page.Page;
import com.zoo.diannaopeijian.domain.Ware;
import com.zoo.diannaopeijian.service.WareService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 商品控制器
 */
@Controller
@RequestMapping(value = "/ware")
public class WareController extends BaseController {
    @Resource
    private WareService wareService;

    @GetMapping(value = "/pages/{pageName}")
    public String pageRoute(@PathVariable String pageName) {
        return "ware/" + pageName;
    }

//    @RequestMapping(value = "WareManaPage")
//    public String WareManaPageRoute() {
//        return "ware/WareManaPage";
//    }
//
//    @RequestMapping(value = "WareAddPage")
//    public String WareAddPageRoute() {
//        return "ware/WareAddPage";
//    }
//
//    @RequestMapping(value = "WareUpdatePage")
//    public String WareUpdatePageRoute() {
//        return "ware/WareUpdatePage";
//    }
//
//    @RequestMapping(value = "WareComponentManaPage")
//    public String WareComponentManaPageRoute() {
//        return "ware/WareComponentManaPage";
//    }

    @GetMapping(value = "/list")
    public ResponseEntity<Map<String, Object>> wareListGet(
            Page page,
            @RequestParam(required = true, defaultValue = "false") boolean isFuzzy,
            Ware ware
    ) {
        return ResponseEntity.ok(buildPageResult(isFuzzy ? wareService.getFuzzyWareWithPage(page, ware) : wareService.getWareWithPage(page, ware)));
    }

    @GetMapping(value = "listByNameAndBarCode")
    public ResponseEntity<List<Ware>> wareListByNameAndBarCodeGet(String q) {
        new Page().onPage();

        return ResponseEntity.ok(wareService.getWareByNameAndBarCode(new Ware(null, q, null, q)));
    }

    @PostMapping(value = "/")
    public ResponseEntity<String> warePost(Ware ware) {
        return ResponseEntity.ok(wareService.addWare(ware) ? success : fail);
    }

    @DeleteMapping(value = "/{wareId}")
    public ResponseEntity<String> wareDelete(@PathVariable Integer wareId) {
        return ResponseEntity.ok(wareService.deleteWareById(wareId) ? success : fail);
    }

    @PutMapping(value = "/")
    public ResponseEntity<String> warePut(Ware ware) {
        return ResponseEntity.ok(wareService.updateWare(ware) ? success : fail);
    }
}
